<?php
class Request{

    protected $key;
    protected $address;

    const DEAL_CREATED = "Deal created";
    const DEAL_NOT_CREATED = "Deal not created";
    const DEAL_INVALID_USER = "User not found";
    const DEAL_INVALID_ACCOUNT = "Account not found";
    const DEAL_INVALID_SIG = "Invalid SIG";

    public function  __construct() {
        global $sugar_config;

        $this->key = $sugar_config['request']['key'];
        $this->address = $sugar_config['request']['address'];

    }

    public function sendAccount($name, $street, $zip, $town, $www, $nip, $regon, $accountEmail){
        global $current_user;

		$ch = curl_init();

		curl_setopt($ch, CURLOPT_URL, $this->address);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_TIMEOUT, 20);
		curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

		$post_data = array(
            'user' => $name,
            'email' => $accountEmail,
            'nip' => $nip,
            'regon' => $regon,
            'con_email' => $current_user->emailAddress->getPrimaryAddress($current_user),
            'address' => $street,
            'zip' => $zip,
            'city' => $town
        );

		if (!empty($www))
			$post_data['www'] = $www;

		$post_data['sig'] = $this->createSig($post_data);

		curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));

		$result = curl_exec($ch);
		curl_close($ch);

        if ($result) {
             $message = $result;
         } else {
             $message = 'An error occurred';
         }

         return $message;
    }

    /**
     *
     * @global <type> $current_user
     * @param Account $account
     * @return <type>
     *
     */
    public function createSig($data){
        return md5(http_build_query($data).$this->key);
    }

}

